package com.gc.repository.impl;

import com.gc.entity.BookSeller;
import com.gc.entity.Student;
import com.gc.repository.BookSellerRepository;
import com.gc.util.C3p0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class BookSellerRepositoryImpl implements BookSellerRepository {
    @Override
    public boolean Add(BookSeller bookSeller) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            qr.update("insert into bookseller(name, bid,  password, phone, email) VALUES (?, ?, ?, ?, ?)",
                    bookSeller.getName(),
                    bookSeller.getBid(),
                    bookSeller.getPassword(),
                    bookSeller.getPhone(),
                    bookSeller.getEmail()
            );
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<BookSeller> FindAll() {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            List<BookSeller> query = qr.query("select * from bookseller", new BeanListHandler<>(BookSeller.class));

            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean DelById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("delete from bookseller WHERE id = ?", id);
            System.out.println(res);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public BookSeller FindById(Long id) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            BookSeller res = qr.query("select * from bookseller WHERE id = ?", new BeanHandler<>(BookSeller.class),
                    id);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public BookSeller FindByBookSellerId(String bid) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            BookSeller res = qr.query("select * from bookseller WHERE bid = ?", new BeanHandler<>(BookSeller.class),
                    bid);
            System.out.println(res);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean UpdateById(BookSeller bookSeller) {
        QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());
        try {
            int res = qr.update("update bookseller set name = ?, bid = ?, password = ?, phone = ?, email = ? WHERE id = ?",
                    bookSeller.getName(),
                    bookSeller.getBid(),
                    bookSeller.getPassword(),
                    bookSeller.getPhone(),
                    bookSeller.getEmail(),
                    bookSeller.getId());
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
